<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    canvas {
      border: 1px solid #ccc;
    }
  </style>
</head>
<body>
  <canvas class="canvas" width="500" height="500"></canvas>
  <!-- 顶点着色器 -->
  <script id="vertex-shader" type="x-shader/x-vertex">
    attribute vec2 aPos;
    attribute vec2 aPos1;
    void main() {
      gl_PointSize = 10.0;
      vec2 newPos = aPos + aPos1;
      gl_Position = vec4(newPos, 0, 1);
    }
  </script>
  <!-- 片段着色器 -->
  <script id="fragment-shader" type="x-shader/x-fragment">
    void main() {
      gl_FragColor = vec4(1, 0, 0, 1);
    }
  </script>
  <script>
    const canvas = document.querySelector(".canvas");
    const gl = canvas.getContext("webgl");
    // 创建顶点着色器和片段着色器
    const vertexShader = gl.createShader(gl.VERTEX_SHADER);
    const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
    // 获取着色器源码
    const vertexSource = document.getElementById("vertex-shader").innerHTML;
    const fragmentSource = document.getElementById("fragment-shader").innerHTML;
    // 绑定着色器源码
    gl.shaderSource(vertexShader, vertexSource);
    gl.shaderSource(fragmentShader, fragmentSource);
    // 编译着色器源码
    gl.compileShader(vertexShader);
    gl.compileShader(fragmentShader);
    // 创建着色器程序
    const program = gl.createProgram();
    // 绑定顶点着色器和片段着色器
    gl.attachShader(program, vertexShader);
    gl.attachShader(program, fragmentShader);
    // 链接着色器程序
    gl.linkProgram(program);
    // 使用着色器程序
    gl.useProgram(program);

    const posLocation = gl.getAttribLocation(program, "aPos");
    const posLocation1 = gl.getAttribLocation(program, "aPos1");
    console.log(posLocation, posLocation1);
    const offset = [0.5, 0.5];
    const offset1 = [-0.1, 0.2];
    gl.vertexAttrib2f(posLocation, ...offset);
    gl.vertexAttrib2f(posLocation1, ...offset1);

    // 绘制图元
    gl.drawArrays(gl.POINTS, 0, 1);
  </script>
</body>
</html>